// pages/business/order/order.js
var utils = require('../../../utils/util.js');
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        ishide: true,
        currentPage: 1,
        orderState: 10000
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
       
    },
    onShow:function(){
        var that=this;
        that.categorylist(that.data.currentPage, that.data.orderState)
    },
    categorylist: function (currentPage, orderState) {
        var that = this;
        utils.http("/api/v1/Storemanage/my_order", { store_id: wx.getStorageSync("userType").storeid, page: currentPage, order_state: orderState }, function (res) {
            for (var i in res.datas) {
                var namepost = [];
                for (var j in res.datas[i].snap_items) {
                    namepost.push(res.datas[i].snap_items[j].name)
                }
                res.datas[i].namepost = namepost.join(' / ');
            }
            utils.hideLoading();
            var allpost = that.data.list;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                allpost.push(res.datas[i]);
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                list: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
        })
    },
    /**
* 页面上拉触底事件的处理函数
*/
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.categorylist(page, that.data.orderState)
        }

    },
    //拨打电话
    bindPhoneTap: function (e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.mobile //仅为示例，并非真实的电话号码
        })
    },
    //tab切换
    tab: function (e) {
        var that = this;
        var stateId = e.currentTarget.dataset.stateid;
        var dataId = e.currentTarget.id;
        var curHdIndex = dataId;
        console.log(e)
        this.setData({
            curHdIndex: curHdIndex,
            orderState: stateId,
            currentPage: 1,
            list: []
        })
        that.categorylist(that.data.currentPage, that.data.orderState)
    },
    //订单页
    onBusinessTap: function (e) {
        wx.reLaunch({
            url: '../../business/business',
        })
    },

    //点击自提核销
    onzitiOrderTap: function (e) {
        var that = this;
        that.setData({
            ishide: false,
            order_id: e.currentTarget.dataset.id
        })
    },
    //关闭自提核销
    onCosleTap: function (e) {
        var that = this;
        that.setData({
            ishide: true
        })
    },
    //onBusPersonalTap
    onBusPersonalTap: function (e) {
        wx.reLaunch({
            url: '../buspersonal/buspersonal',
        })
    },
    //扫码
    butareaHover: function (e) {
        var that = this;
        wx.scanCode({
            success: (res) => {
                var resList = res.result.split("@");
                that.setData({
                    resList: resList[1]
                })

               
            }
        })
    },
    formSubmit: function (e) {
        var that = this;
        console.log(that.data.order_id)
        utils.http("/api/v1/storemanage/write_off", {
            order_id: that.data.order_id,
            verification: e.detail.value.resList,
            store_id: wx.getStorageSync("userType").storeid,
        }, function (res) {
            console.log(res);
            if (res.state == 1) {
                wx.showToast({
                    title: '扫码核销成功，已确认收货',
                })
                that.setData({
                    ishide: true,
                    order_id:"",
                    resList:"",
                    list: []
                })
                that.categorylist(1, that.data.orderState)
            } else {
                that.setData({
                    resList: "",
                })
                utils.message(res.datas);
            }
        })
    }
})